<template>
  <el-row class="home" :gutter="20">
    <el-col :span="8" style="margin-top: 20px">
      <el-card shadow="hover">
        <div class="user">
          <img :src="imgaeUrl" />
          <div class="userinfo">
            <p class="name">Admin</p>
            <p class="access">管理员</p>
          </div>
        </div>
        <div class="login-info">
          <p>上次登录的时间：<span>2022-7-19</span></p>
          <p>上次登录的地点：<span>北京</span></p>
        </div>
      </el-card>
      <el-card style="margin-top: 20px; height: 460px">
        <el-table :data="tableData">
          <el-table-column
            v-for="(val, key) in tableLabel"
            :key="key"
            :prop="key"
            :label="val"
          ></el-table-column>
        </el-table>
      </el-card>
    </el-col>
    <el-col style="margin-top: 20px" :span="16">
      <div class="num">
        <el-card
          v-for="item in countData"
          :key="item.name"
          :body-style="{ display: 'flex', padding: 0 }"
        >
          <i
            class="icon"
            :class="`el-icon-${item.icon}`"
            :style="{ background: item.color }"
          ></i>
          <div class="detail">
            <p class="num">￥{{ item.value }}</p>
            <p class="txt">{{ item.name }}</p>
          </div>
        </el-card>
      </div>
      <el-card style="height: 280px">
        <div style="height: 280px" ref="echarts"></div>
      </el-card>
      <div class="graph">
        <el-card style="height: 260px">
          <div style="height: 240px" ref="userEcharts"></div>
        </el-card>
        <el-card style="height: 260px">
          <div style="height: 240px" ref="videoEcharts"></div>
        </el-card>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import { getUserInfo, editUserInfo } from "../../api/data";
import Mock from "mockjs";
import * as echarts from "echarts";
export default {
  name: "home",
  data() {
    return {
      // userImg: require("../../assets/images/user.png"),
      imgaeUrl: "",
      orderData: [],
      tableData: [
        {
          name: "饮料",
          todayBuy: 100,
          monthBuy: 300,
          totalBuy: 800,
        },
        {
          name: "膨化",
          todayBuy: 100,
          monthBuy: 300,
          totalBuy: 800,
        },
        {
          name: "卤味",
          todayBuy: 100,
          monthBuy: 300,
          totalBuy: 800,
        },
        {
          name: "甜点",
          todayBuy: 100,
          monthBuy: 300,
          totalBuy: 800,
        },
        {
          name: "速食",
          todayBuy: 100,
          monthBuy: 300,
          totalBuy: 800,
        },
        {
          name: "雪糕",
          todayBuy: 100,
          monthBuy: 300,
          totalBuy: 800,
        },
      ],
      videoData: [
        {
          name: "达利园",
          value: 2999,
        },
        {
          name: "哇哈哈",
          value: 5999,
        },
        {
          name: "旺旺",
          value: 1500,
        },
        {
          name: "康师傅",
          value: 1999,
        },
        {
          name: "统一",
          value: 2200,
        },
        {
          name: "乐事",
          value: 4500,
        },
      ],
      userData: [
        {
          date: "周一",
          new: 5,
          active: 200,
        },
        {
          date: "周二",
          new: 10,
          active: 500,
        },
        {
          date: "周三",
          new: 12,
          active: 550,
        },
        {
          date: "周四",
          new: 60,
          active: 800,
        },
        {
          date: "周五",
          new: 65,
          active: 550,
        },
        {
          date: "周六",
          new: 53,
          active: 770,
        },
        {
          date: "周日",
          new: 33,
          active: 170,
        },
      ],
      tableLabel: {
        name: "课程",
        todayBuy: "今日购买",
        monthBuy: "本月购买",
        totalBuy: "总购买",
      },
      countData: [
        {
          name: "今日支付订单",
          value: 1234,
          icon: "success",
          color: "#2ec7c9",
        },
        {
          name: "今日收藏订单",
          value: 210,
          icon: "star-on",
          color: "#ffb980",
        },
        {
          name: "今日未支付订单",
          value: 1234,
          icon: "s-goods",
          color: "#5ab1ef",
        },
        {
          name: "本月支付订单",
          value: 1234,
          icon: "success",
          color: "#2ec7c9",
        },
        {
          name: "本月收藏订单",
          value: 210,
          icon: "star-on",
          color: "#ffb980",
        },
        {
          name: "本月未支付订单",
          value: 1234,
          icon: "s-goods",
          color: "#5ab1ef",
        },
      ],
    };
  },
  methods: {
    needData() {
      let List = [];
      for (let i = 0; i < 7; i++) {
        List.push(
          Mock.mock({
            干果: Mock.Random.float(100, 8000, 0, 0),
            糖果: Mock.Random.float(100, 8000, 0, 0),
            果冻: Mock.Random.float(100, 8000, 0, 0),
            豆制品: Mock.Random.float(100, 8000, 0, 0),
            瓜子: Mock.Random.float(100, 8000, 0, 0),
            休闲食品: Mock.Random.float(100, 8000, 0, 0),
          })
        );
      }
      let orderData = {
        date: [
          "20191001",
          "20191002",
          "20191003",
          "20191004",
          "20191005",
          "20191006",
          "20191007",
        ],
        data: List,
      };
      this.orderData = orderData;
    },
    getData() {
      const order = this.orderData;
      const xData = order.date;
      const keyArray = Object.keys(order.data[0]);
      const series = [];
      keyArray.forEach((key) => {
        series.push({
          name: key,
          data: order.data.map((item) => item[key]),
          type: "line",
        });
      });
      //折线图
      const option = {
        xAxis: {
          data: xData,
        },
        yAxis: {},
        legend: {
          data: keyArray,
        },
        series,
      };

      const E = echarts.init(this.$refs.echarts);
      E.setOption(option);
      //用户图
      const userOprion = {
        legend: {
          // 图例文字颜色
          textStyle: {
            color: "#333",
          },
        },
        grid: {
          left: "20%",
        },
        // 提示框
        tooltip: {
          trigger: "axis",
        },
        xAxis: {
          type: "category", // 类目轴
          data: this.userData.map((item) => item.date),
          axisLine: {
            lineStyle: {
              color: "#17b3a3",
            },
          },
          axisLabel: {
            interval: 0,
            color: "#333",
          },
        },
        yAxis: [
          {
            type: "value",
            axisLine: {
              lineStyle: {
                color: "#17b3a3",
              },
            },
          },
        ],
        color: ["#2ec7c9", "#b6a2de"],
        series: [
          {
            name: "新增用户",
            data: this.userData.map((item) => item.new),
            type: "bar",
          },
          {
            name: "活跃用户",
            data: this.userData.map((item) => item.active),
            type: "bar",
          },
        ],
      };

      const U = echarts.init(this.$refs.userEcharts);
      U.setOption(userOprion);
      //饼图
      const videoOption = {
        tooltip: {
          trigger: "item",
        },
        color: [
          "#0f78f4",
          "#dd536b",
          "#9462e5",
          "#a6a6a6",
          "#e1bb22",
          "#39c362",
          "#3ed1cf",
        ],
        series: [
          {
            data: this.videoData,
            type: "pie",
          },
        ],
      };
      const V = echarts.init(this.$refs.videoEcharts);
      V.setOption(videoOption);
    },
  },
  created() {
    this.needData();
    getUserInfo(this.$store.state.user.token).then(({ data: res }) => {
      if (res.status === 0) {
        let stringToDelete = "picture/";
        let originalString = res.data.user_pic;
        let baseUrl = "http://127.0.0.1:3002/";
        let newString = baseUrl + originalString.replace(stringToDelete, "");
        this.imgaeUrl = newString;
      }
    });
  },
  mounted() {
    this.getData();
  },
};
</script>

<style>
</style>